var Category = (function () {
    function Category(_name, _color) {
        this._name = _name;
        this._color = _color;
        this.Color = _color;
        this.Name = _name;
    }
    return Category;
})();
var Entry = (function () {
    function Entry(_startDate) {
        this._startDate = _startDate;
        var _this = this;
        this.Title = ko.observable('');
        this.Description = ko.observable('');
        this.StartDate = ko.observable(_startDate);
        this.EndDate = ko.observable(_startDate);
        this.IsAllDay = ko.observable(true);
        this.DisplayTitle = ko.computed(function () {
            if(_this.IsAllDay()) {
                return _this.Title();
            } else {
                return moment(_this.StartDate()).format('H:mm ') + _this.Title();
            }
        });
    }
    return Entry;
})();
var Day = (function () {
    function Day(date, viewModel) {
        this.date = date;
        this.Date = date;
        this.ViewModel = viewModel;
        this.Entries = ko.observableArray();
    }
    Day.prototype.day_click = function (a, element) {
        var current_entry = this.ViewModel.current_entry;
        current_entry().StartDate(this.Date);
        var vm = this.ViewModel;
        var entries = this.Entries;
        $('#CAL-New-Entry-Dialog').dialog({
            title: 'Create entry',
            autoResize: true,
            width: 400,
            open: function () {
                $('#CAL-NewEntry-Title').keypress(function (event) {
                    if(event.which == 13) {
                        $('.ui-dialog-buttonpane button:last').trigger('click');
                        event.preventDefault();
                    }
                });
            },
            buttons: [
                {
                    text: "Save",
                    click: function () {
                        var title = current_entry().Title();
                        var startDate = current_entry().StartDate();
                        title = $.trim(title);
                        var titleParts = title.split(' ');
                        if(titleParts.length > 0) {
                            var possibleTime = titleParts[0];
                            var extendedDate = moment().format('LL') + ', ' + possibleTime;
                            if(moment(extendedDate).isValid()) {
                                var timeParts = possibleTime.split(':');
                                if(timeParts.length >= 2) {
                                    var hour = parseInt(timeParts[0]);
                                    var minute = parseInt(timeParts[1]);
                                    if(!isNaN(hour) && !isNaN(minute) && hour < 24 && minute < 60 && hour >= 0 && minute >= 0) {
                                        var timeStringLength = timeParts[0].length + timeParts[1].length + 2;
                                        title = title.substring(timeStringLength, title.length);
                                        startDate = moment(startDate).hours(hour).minutes(minute);
                                        console.log(startDate.format('YYYY MM DD H:mm') + ' ' + title);
                                        current_entry().IsAllDay(false);
                                    }
                                }
                            }
                        }
                        current_entry().StartDate(startDate);
                        current_entry().Title(title);
                        entries.push(current_entry());
                        vm.current_entry(new Entry(moment()));
                        $(this).dialog("close");
                    }
                }
            ]
        });
    };
    Day.prototype.addEntry = function () {
    };
    return Day;
})();
var Week = (function () {
    function Week(firstDayofWeek, viewModel) {
        this.firstDayofWeek = firstDayofWeek;
        this.FirstDay = firstDayofWeek;
        this.ViewModel = viewModel;
        this.Days = ko.observableArray();
        this.Days.push(new Day(moment(firstDayofWeek), this.ViewModel));
        for(var i = 1; i < 7; i++) {
            this.Days.push(new Day(moment(firstDayofWeek.add('d', 1)), this.ViewModel));
        }
    }
    return Week;
})();
var ViewModel = (function () {
    function ViewModel(_year, _month) {
        this._year = _year;
        this._month = _month;
        var now = new Date(Date.now());
        this.current_entry = ko.observable(new Entry(moment()));
        this.set_self(this);
        var monthFirstDay = moment([
            _year, 
            _month - 1
        ]);
        var weekDay = monthFirstDay.format('d');
        var daysFromPreviousMonth = 0;
        switch(weekDay) {
            case '0': {
                daysFromPreviousMonth = 6;
                break;
            }

            case '1': {
                daysFromPreviousMonth = 5;
                break;
            }

            case '2': {
                daysFromPreviousMonth = 4;
                break;
            }

            case '3': {
                daysFromPreviousMonth = 3;
                break;
            }

            case '4': {
                daysFromPreviousMonth = 2;
                break;
            }

            case '5': {
                daysFromPreviousMonth = 1;
                break;
            }

            case '6': {
                daysFromPreviousMonth = 0;
                break;
            }

        }
        var firstRenderDay = monthFirstDay.add('days', -1 * (daysFromPreviousMonth + 1));
        this.weeks = ko.observableArray();
        this.weeks.push(new Week(moment(firstRenderDay), this));
        for(var i = 1; i < 5; i++) {
            var w = moment(moment(firstRenderDay.add('w', 1)));
            this.weeks.push(new Week(w, this));
        }
        this.categories = ko.observableArray([
            new Category("Important", "#9A81B5"), 
            new Category("Normal", "#ADCB52"), 
            new Category("Lazy", "#F9C33F")
        ]);
    }
    ViewModel.prototype.set_self = function (_vm) {
        this.vm_self = _vm;
    };
    return ViewModel;
})();
function StringToDate(text) {
    var n = Date.parse(text);
    return new Date(n);
}
$(function () {
    var vm5 = new ViewModel(2012, 11);
    ko.applyBindings(vm5);
});
function updateViewModel() {
    ko.applyBindings(new ViewModel(2012, 11));
}
